/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-17 16:00:41
 ******************************************************************************/
package cn.cmcc.online.smsapi.sdk.queue;

import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import cn.cmcc.online.smsapi.sdk.Iservice.OnlineUpdateCycleConfigInterface;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.entity.CountSceneManager;
import cn.cmcc.online.smsapi.sdk.db.entity.PopupActionManager;
import cn.cmcc.online.smsapi.sdk.db.entity.RecordActionManager;
import cn.cmcc.online.smsapi.sdk.db.entity.SceneconfigManager;
import cn.cmcc.online.smsapi.sdk.db.entity.SmsParseRecorder;
import cn.cmcc.online.smsapi.sdk.db.entity.menuActionManager;
import cn.cmcc.online.smsapi.sdk.db.entity.pubinfo.NumNameManager;
import cn.cmcc.online.smsapi.sdk.db.entity.pubinfo.ShardDataManager;
import cn.cmcc.online.smsapi.sdk.dex.DexUtil;
import cn.cmcc.online.smsapi.sdk.iccid.IccidLocationUtil;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.service.pubId.ParsePhonePubIdService;
import cn.cmcc.online.smsapi.sdk.service.pubInfo.PubInfoNetService;
import cn.cmcc.online.smsapi.sdk.util.InitUtil;
import cn.cmcc.online.smsapi.sdk.util.RecordFunctionUtil;

/**
 * 任务队列
 * 
 * @author Administrator
 * 
 */
public class SdkTaskQueue {

    public static BlockingQueue<TaskInfo> queue = new LinkedBlockingQueue<TaskInfo>();// 阻塞队列
    public static int XIAOYUAN_THREA_PRIORITY = 10;

    public static void addTask(TaskInfo data) {
        try {
            queue.put(data);
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "addTask: ", e);

        }

    }

    static Thread queueThread = null;

    public synchronized static void startTaskQueue() {
        if (queueThread == null) {
            queueThread = new Thread() {
                @Override
                public void run() {
                	try {
                		this.setName("xiaoyuan_taskqueue");
                    android.os.Process
                            .setThreadPriority(XIAOYUAN_THREA_PRIORITY);
                    while (true) {
                        try {
                            LogManager.d("SdkTaskQueue", "run startTaskQueue");
                            /*SDK-789 zhaojiangwei 20170605 start */
                            TaskInfo task = queue.take();
                            /*SDK-789 zhaojiangwei 20170605 end */
                            if (task != null) {
                                switch (task.type) {
                                /*
                                 * case 0://检测正则入库
                                 * ParseItemManager.checkHasData(); break;
                                 */
                                case 1:// 查询归属地
                                    IccidLocationUtil.startQueryIccidLocation(
                                            task.paramsMap, true);
                                    break;
                                case 2:// 检测初始化
                                    InitUtil.checkInit();
                                    break;
                                case 3:// 解析成功，统计打点
                                    PopupActionManager
                                            .insertOrUpdateParse(task.paramsMap);
                                    break;
                                case 4:// 弹窗成功，统计打点
                                    PopupActionManager
                                            .insertOrUpdatePopup(task.paramsMap);
                                    break;
                                case 5:// 操作按钮点击成功，统计打点
                                    RecordActionManager
                                            .insertOrUpdateParse(task.paramsMap);
                                    break;
                                case 6:// 解析成功，记录上报情景号次数
                                    CountSceneManager
                                            .insertOrUpdate(task.paramsMap);
                                    break;
                                case 7:
                                    // 检查是否需要下载logo
                                    ResourseQueue.runResourseQueue();
                                    break;
                                case 8:// 解析成功，记录上报情景号次数
                                    SceneconfigManager
                                            .insertOrUpdate(task.paramsMap);
                                    /*SDK-431 songzhirong 20160530 start*/
                                    ReportLogQueue.runReportLogQueue();
                                    /*SDK-431 songzhirong 20160530 end*/
                                    break;
                                case 9:// 解析企业签名及行业编码
                                    NumNameManager
                                            .parseNumNameAndEcForMsg(task.paramsMap);
                                    EmergencyQueue.runEmergencyQueue();
                                    /* RM-1351 huangzhiqiang 20170407 start */
                                    PubInfoNetService.uploadPubInfoSignNew();
//                                  PubInfoNetService.uploadPubInfoSign();
                                    /* RM-1351 huangzhiqiang 20170407 end */
                                 
                                    break;
                                    
                                case 10:// 退订上报                                  
                                    EmergencyQueue.runEmergencyQueue();
                                    // 退订指令上传
                                    PubInfoNetService.uploadPubInfoCmd();
                                    break;
                                case 11:
                                    // 菜单打点统计
                                	menuActionManager
                                    .insertOrUpdateParse(task.paramsMap);
                                	break;
                                case 12:
                                	// 功能标记状态
                                	RecordFunctionUtil.setFunctionState(Integer.parseInt(task.paramsMap.get("state")));
                                	break;
                                case 13:
                                	// 异步查询pubid
                                	ParsePhonePubIdService.queryPubIdService(task.paramsMap.get("phoneNum"), 
                                			task.paramsMap.get("dbresoult"));
//                                	RecordFunctionUtil.setFunctionState(Integer.parseInt(task.paramsMap.get("state")));
                                	break;
                                	
                                case 14:
                                    //  增加记录
                                    Map<String, String> paramsMap = task.paramsMap; 
                                    String phoneNumber = paramsMap.get("phoneNumber");
                                    boolean isSuccess = Boolean.valueOf(paramsMap.get("isSuccess"));
                                    SmsParseRecorder.addRecord(phoneNumber, isSuccess);
                                    break;
                                    /* SDK-383 特征值识别解析写入数据库  xusongzhou 20160412 start */
                                case 15:                                
                                    break;
                                    /* SDK-383 特征值识别解析写入数据库  xusongzhou 20160412 end */
                                case 16:
                                        /* SDK-421 huangzhiqiang 20160525 start */
                                        // 删除一个月之前的旧数据
                                        ShardDataManager.deleteBeforeLastTime(
                                                System.currentTimeMillis() - DexUtil.getUpdateCycleByType(
                                                        OnlineUpdateCycleConfigInterface.TYPE_HISTORY_SHARD_DELETE_CYCLE,
                                                        Constant.month));
                                        // 分片数据去敏后原文签名入库
                                        ShardDataManager.parseShardSignForMsg(task.paramsMap);
                                        // 分片数据上传
                                        PubInfoNetService.uploadShard();
                                        /* SDK-421 huangzhiqiang 20160525 end */
                                    break;
                                }
                            }
                            /*SDK-789 zhaojiangwei 20170605 start */
                            task = null;
                            /*SDK-789 zhaojiangwei 20170605 end */
                        } catch (Throwable e) {
//                            e.printStackTrace();
                            LogManager.e(Constant.TAG, "startTaskQueue: ", e);

                        }
                    }
                
					} catch (Throwable e) {
						LogManager.e(Constant.TAG, e.getMessage(), e);
					}
                    };
            };
            queueThread.start();
        }

    }
}
